package com.doordu.soa.service.web.fieldvalidator;

import com.doordu.soa.service.web.annotation.DataValid;
import com.google.common.base.Joiner;
import com.google.common.primitives.Ints;

import java.util.Arrays;

/**
 * Created by zjp on 2017/4/19.
 */
public class IntegerChoiceHandler implements BaseValidatorHandler<Number> {

    @Override
    public String doValidate(DataValid dataValid, Object bean, Number value) {
        int[] choiceArray = dataValid.choice();
        int longValue = value.intValue();
        boolean nullFlag = dataValid.nullable();
        if(!nullFlag || (nullFlag && longValue>0)){
        	 if (Arrays.binarySearch(choiceArray, longValue) < -1) {
                 String errMsg = Joiner.on(",").join(Ints.asList(choiceArray));
                 return String.format("可选值为:[%s]", errMsg);
             }
        }
       
        return PASS;
    }
}
